<template>
  <ModalFormTemplate
    :open="open"
    :formConfig="formConfig"
    :title="title"
    :width="width"
    @cancel="onCancel"
    @finish="onFinish"
    ref="modalRef"
  />
</template>
<script setup lang="ts">
  import { computed, ref, watch } from 'vue';
  import ModalFormTemplate from '@/components/template/form/modal-form-template.vue';

  const title = '服务商修改'; //弹窗标题
  const width = 550; //弹窗宽度
  const modalRef = ref(); //通用模态弹窗组件引用，用与调用模态弹窗组件内部方法
  const open = ref(false); //控制弹窗显示、隐藏
  const emits = defineEmits<{ dataReload: [] }>(); //通知调用组件(父组件) 执行 父组件 的 @dataReload 的方法
  const editId = ref(0); //要修改的记录ID

  defineExpose({ show }); //暴露接口 show(显示弹窗) 方法，供 父组件 调用

  /* [关闭弹窗] 1. 重置表单 2. 关闭弹窗 */
  function onCancel() {
    modalRef.value.resetForm();
    open.value = false;
  }

  /* [显示弹窗] 1. 接收参数 2.显示弹窗  */
  function show(id: number) {
    editId.value = id;
    open.value = true;
  }

  /* [监听弹窗显示] open = true 时，调用 getDataInfo 获取数据 */
  watch(
    () => open.value,
    (val) => {
      if (val === true) {
        getDataInfo();
      }
    }
  );

  /* [获取修改数据] 调用子组件 getDataInfo 方法详情 */
  function getDataInfo() {
    modalRef.value?.getDataInfo(getAction, getParams.value);
  }
  const getAction = 'base.manage.isv.ServiceImpl.get'; //get接口 方法名
  const getParams = computed(() => ({ isvId: editId.value })); //get接口 参数

  /* [提交弹窗] 1. 调用子组件 提交方法 2. 提交成功后，关闭弹窗 3. 刷新父组件数据 */
  async function onFinish() {
    const params = modalRef.value?.getFormParams();
    const mergedParams = { ...getParams.value, ...params }; //合并提交参数
    try {
      const ret = await modalRef.value?.postFormData(updateAction, mergedParams);
      if (ret === true) {
        onCancel();
        emits('dataReload');
      }
    } catch (error) {}
  }
  const updateAction = 'base.manage.isv.ServiceImpl.update'; //update接口 方法名

  /* [表单配置] */
  const formConfig = computed(() => [
    {
      title: '服务商名称',
      dataIndex: 'isv_name',
      type: 'input' as const,
      maxlength: 250,
      required: true,
      defaultValue: '',
    },
    {
      title: '服务商简称',
      dataIndex: 'isv_short_name',
      type: 'input' as const,
      maxlength: 50,
      required: true,
      defaultValue: '',
    },
    {
      title: '服务商联系人',
      dataIndex: 'isv_contact_name',
      type: 'input' as const,
      maxlength: 200,
      required: true,
      defaultValue: '',
    },
    {
      title: '服务商联系电话',
      dataIndex: 'isv_contact_phone',
      type: 'input' as const,
      maxlength: 13,
      required: true,
      defaultValue: '',
    },
    {
      title: '服务商联系邮箱',
      dataIndex: 'isv_contact_email',
      type: 'input' as const,
      maxlength: 111,
      required: true,
      defaultValue: '',
    },
    {
      title: '服务商地址',
      dataIndex: 'isv_address',
      type: 'input' as const,
      maxlength: 150,
      required: true,
      defaultValue: '',
    },
    {
      title: '服务商网址',
      dataIndex: 'isv_url',
      type: 'input' as const,
      maxlength: 250,
      defaultValue: '',
    },
    {
      title: '状态',
      dataIndex: 'isv_status',
      type: 'radio' as const,
      props: {
        options: [
          { label: '启用', value: 1 },
          { label: '停用', value: 2 },
        ],
      },
      defaultValue: 1,
    },
    {
      title: '备注',
      dataIndex: 'remark',
      type: 'input' as const,
      maxlength: 250,
      defaultValue: '',
    },
  ]);
</script>
<style lang="less" scoped></style>
